package com.woniuxy.member.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.member.app.apiImpl.util.RequestUtil;
import com.woniuxy.member.client.CommentManageClient;
import com.woniuxy.member.client.param.AddCommentParam;
import com.woniuxy.member.service.CommentInfoService;
import com.woniuxy.member.service.param.AddCommentServiceParam;
import com.woniuxy.orderService.client.OrderManageClient;
import com.woniuxy.orderService.client.OrderQueryClient;
import com.woniuxy.orderService.client.result.MemberQueryPayOrderDto;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.CommonResultCodeEnum;
import result.Result;

import javax.validation.Valid;

@RequestMapping("/member")
@RestController
public class CommentManageApiImpl implements CommentManageClient {
    @Autowired
    private CommentInfoService commentInfoService;
    @Autowired
    private OrderQueryClient orderQueryClient;
    @Autowired
    private OrderManageClient orderManageClient;
    @Log("用户添加评论")
    @Override
    public Result addComment(@Valid AddCommentParam addCommentParam) {
        Result<MemberQueryPayOrderDto> memberQueryPayOrderDtoResult = orderQueryClient.memberQueryPayOrder(addCommentParam.getPayOrderId());
        if (memberQueryPayOrderDtoResult.getData().getMemberId()!= RequestUtil.getMemberId()||!memberQueryPayOrderDtoResult.getData().getPayState().equals("PAID")) {
            throw new MallException("评论失败", CommonResultCodeEnum.INVALID_PARAM);
        }
        orderManageClient.modifyPayOrderCommentState(addCommentParam.getPayOrderId());
        AddCommentServiceParam addCommentServiceParam = BeanUtil.toBean(addCommentParam, AddCommentServiceParam.class);
        addCommentServiceParam.setStoreId(RequestUtil.getStoreId());
        addCommentServiceParam.setMemberId(RequestUtil.getMemberId());
        commentInfoService.addComment(addCommentServiceParam);
        return Result.success("用户添加评论成功");
    }
    @Log("商家删除评论")
    @Override
    public Result delStoreComment(Integer storeId,Integer commentId) {
        commentInfoService.delStoreComment(storeId,commentId);
        return Result.success("商家删除评论成功");
    }
    @Log("用户删除自己评论")
    @Override
    public Result delComment(Integer commentId) {
        commentInfoService.delComment(RequestUtil.getMemberId(),RequestUtil.getStoreId(),commentId);
        return Result.success("用户删除自己评论成功");
    }
}
